<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.11"/>
<title>GrPPI: Stencil pattern</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { init_search(); });
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="logo.svg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">GrPPI
   &#160;<span id="projectnumber">0.2</span>
   </div>
   <div id="projectbrief">Generic and Reusable Parallel Pattern Interface</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.11 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li class="current"><a href="pages.html"><span>Related&#160;Pages</span></a></li>
      <li><a href="modules.html"><span>Modules</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Stencil pattern </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>The <b>stencil</b> pattern applies a transformation to every element in one or multiple data sets, generating a new data set as an output. The transformation also takes as an input a neighborhood of the transformed data item.</p>
<p>The interface to the <b>stencil</b> pattern is provided by function <code><a class="el" href="group__stencil__pattern__native.html#ga4c11627ced8c65f6cf4a67fdb4869d3d" title="Invoke Stencil pattern on a data sequence with native parallel execution. ">grppi::stencil()</a></code>. As all functions in <em>GrPPI</em>, this function takes as its first argument an execution policy.</p>
<div class="fragment"><div class="line"><a class="code" href="group__stencil__pattern__native.html#ga4c11627ced8c65f6cf4a67fdb4869d3d">grppi::stencil</a>(exec, other_arguments...);</div></div><!-- fragment --><h2>Stencil variants</h2>
<p>There are several variants:</p>
<ul>
<li><em>Unary stencil</em>: A stencil taking a single input sequence.</li>
<li><em>N-ary stencil</em>: A stencil taking multiple input sequences.</li>
</ul>
<h2>Key elements in stencil</h2>
<p>The key elements in a <b>stencil</b> are: a <b>StencilTransformer</b> operation and a <b>Neighbourhood</b> operation.</p>
<p>A <b>Neighbourhood</b> operation is any C++ callable entity that takes one or more iterators to data items and generates a neighbourhood. Depending on the number of iterators taking it may be a <b>UnaryNeighbourhood</b> or a <b>MultiNeighbourhood</b>.</p>
<p>Consequently, a <b>UnaryNeighbourhood</b> is any operation <code>op</code> that given an iterator value <code>it</code> and an output neighbourhood type <code>N</code> makes valid the following:</p>
<div class="fragment"><div class="line">N n = op(it);</div></div><!-- fragment --><p>A <b>MultiNeighbourhood</b> is any operation <code>op</code> that given <b>n</b> iterator values <code>it1</code>, <code>it2</code>, ... , <code>itN</code> and an output neighbourhood type <code>N</code> makes valid the following:</p>
<div class="fragment"><div class="line">N n = op(it1, it2, ..., itN);</div></div><!-- fragment --><p>The <b>StencilTransformer</b> is any C++ callable entity that takes one iterator to a data item and the result of a <b>Neighbourhood</b> operation and performs a transformation.</p>
<p>Thus, a <b>StencilTransfomer</b> is any operation <code>op</code> that, given an iterator value <code>it</code>and the result of a <b>Neighbourhood</b> operation <code>n</code>, makes valid the following:</p>
<div class="fragment"><div class="line">R r = op(it, n);</div></div><!-- fragment --><h2>Details on stencil variants</h2>
<h3>Unary stencil</h3>
<p>An unary <b>stencil</b> takes a data set and transforms each element in the data set by applying a transformation to each data item using the data item and its neighbourhood.</p>
<p>The only interface currently offered for this pattern is based in iterators (following the C++ standard library conventions):</p>
<ul>
<li>The input data set is specified by two iterators.</li>
<li>The output data set is specified by an iterator to the start of the output sequence. <hr/>
 <b>Example</b>: Stencil operation adding neighbours in a vector. <div class="fragment"><div class="line">vector&lt;double&gt; v = get_the_vector();</div><div class="line">vector&lt;double&gt; w(v.size());</div><div class="line"><a class="code" href="group__stencil__pattern__native.html#ga4c11627ced8c65f6cf4a67fdb4869d3d">grppi::stencil</a>(ex, begin(v), end(v), begin(w),</div><div class="line">    <span class="comment">// Add current element and neighbours</span></div><div class="line">    [](<span class="keyword">auto</span> it, <span class="keyword">auto</span> n) {</div><div class="line">      <span class="keywordflow">return</span> *it + accumulate(begin(n), end(n)); </div><div class="line">    }</div><div class="line">    <span class="comment">// Neighbours are prev and next</span></div><div class="line">    [&amp;](<span class="keyword">auto</span> it) {</div><div class="line">      vector&lt;double&gt; r;</div><div class="line">      <span class="keywordflow">if</span> (it!=begin(v)) r.push_back(*prev(it));</div><div class="line">      <span class="keywordflow">if</span> (distance(it,end(end))&gt;1) r.push_back(*next(it));</div><div class="line">      <span class="keywordflow">return</span> r;</div><div class="line">    }</div></div><!-- fragment --><hr/>
</li>
</ul>
<h3>N-ary stencil</h3>
<p>An n-ary <b>stencil</b> takes multiple data sets and transforms each element in the data set by applying a transformation to each data item form the first data set and the neighbourhood obtained from all data sets.</p>
<p>The only interface currently offered for this pattern is based in iterators:</p>
<ul>
<li>The first data set is specified by two iterators.</li>
<li>The output data set is specified by an iterator to the start of the output sequence.</li>
<li>All the other input data sets are specified by iterators to the start of the input data sequences. <hr/>
 <b>Example</b>: Stencil operation adding neighbours in two vector. <div class="fragment"><div class="line">vector&lt;double&gt; v1 = get_the_vector();</div><div class="line">vector&lt;double&gt; v2 = get_the_vector();</div><div class="line">vector&lt;double&gt; w(v.size());</div><div class="line"></div><div class="line"><span class="keyword">auto</span> get_around = [](<span class="keyword">auto</span> i, <span class="keyword">auto</span> b, <span class="keyword">auto</span> e) {</div><div class="line">};</div><div class="line"></div><div class="line"><a class="code" href="group__stencil__pattern__native.html#ga4c11627ced8c65f6cf4a67fdb4869d3d">grppi::stencil</a>(ex, begin(v), end(v), begin(w),</div><div class="line">    <span class="comment">// Add current element and neighbours</span></div><div class="line">    [](<span class="keyword">auto</span> it, <span class="keyword">auto</span> n) {</div><div class="line">      <span class="keywordflow">return</span> *it + accumulate(begin(n), end(n)); </div><div class="line">    }</div><div class="line">    <span class="comment">// Neighbours are prev and next</span></div><div class="line">    [&amp;](<span class="keyword">auto</span> it1, <span class="keyword">auto</span> it2) {</div><div class="line">      vector&lt;double&gt; r;</div><div class="line">      <span class="keywordflow">if</span> (it1!=begin(v1)) r.push_back(*prev(it1));</div><div class="line">      <span class="keywordflow">if</span> (distance(it1,end(v1))&gt;1) r.push_back(*next(it1));</div><div class="line">      <span class="keywordflow">if</span> (it2!=begin(v2)) r.push_back(*prev(it2));</div><div class="line">      <span class="keywordflow">if</span> (distance(it2,end(v2))&gt;2) r.push_back(*next(it2));</div><div class="line">      <span class="keywordflow">return</span> r;</div><div class="line">    }</div><div class="line">);</div></div><!-- fragment --><hr/>
 </li>
</ul>
</div></div><!-- contents -->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.8.11
</small></address>
</body>
</html>
